package imba.cloud;

import hudson.model.*

def checkOutFrom(repo) {
  pipeline {
    steps.node() {
      def time = System.currentTimeMillis()
      stage('Checkout') {
        git url: "git@gitee.com:imbacloud/${repo}"
      }
      stage('Build') {
        def exists = fileExists 'build.sh'
        if (exists) {
          sh 'bash build.sh'
          echo 'build Yes'
        } 
      }
      stage('Test') {
        echo 'Test OK'
      }
      stage('Publish') {
        def exists = fileExists 'Dockerfile'
        if (exists) {
          def cmd = '''
          url=registry.cn-hangzhou.aliyuncs.com
          docker build -t ${url}/bill080307/${image}:${tag} .
          docker push ${url}/bill080307/${image}
          '''
          cmd = "image=${repo} \n"+ cmd
          cmd = "tag=master-${time} \n"+ cmd
          sh cmd
          echo 'Dockerfile Yes'
        }
      }
      stage('Deploy') {
        def exists = fileExists 'deploy.yml'
        if (exists) {
          def cmd = '''
          scp deploy-out.yml root@172.16.2.128:/tmp/deploy.yml && \
          ssh root@172.16.2.128 'kubectl apply -f /tmp/deploy.yml'
          '''
          cmd = "cp deploy.yml deploy-out.yml; sed -i 's#\$DOMAIN#home.dlimba.top#g' deploy-out.yml; sed -i 's#\$TAG#master-${time}#g' deploy-out.yml \n"+ cmd
          sh cmd
          echo 'deploy Yes'
        }
      }
    }
  }
}

return this